package com.richie696.catalina;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.function.Function;

public class ParameterMap<K, V> extends HashMap<K, V> {

    private boolean lock;

    public ParameterMap(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor);
    }

    public ParameterMap(int initialCapacity) {
        super(initialCapacity);
    }

    public ParameterMap() {
    }

    public ParameterMap(Map<? extends K, ? extends V> m) {
        super(m);
    }

    @Override
    public V put(K key, V value) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        return super.put(key, value);
    }

    @Override
    public void putAll(Map<? extends K, ? extends V> m) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        super.putAll(m);
    }

    @Override
    public V remove(Object key) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        return super.remove(key);
    }

    @Override
    public Set<K> keySet() {
        return super.keySet();
    }

    @Override
    public Collection<V> values() {
        return super.values();
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        return super.entrySet();
    }

    @Override
    public V putIfAbsent(K key, V value) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        return super.putIfAbsent(key, value);
    }

    @Override
    public boolean remove(Object key, Object value) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        return super.remove(key, value);
    }

    @Override
    public boolean replace(K key, V oldValue, V newValue) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        return super.replace(key, oldValue, newValue);
    }

    @Override
    public V replace(K key, V value) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        return super.replace(key, value);
    }

    @Override
    public V computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        return super.computeIfAbsent(key, mappingFunction);
    }

    @Override
    public V computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        return super.computeIfPresent(key, remappingFunction);
    }

    @Override
    public V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        return super.merge(key, value, remappingFunction);
    }

    @Override
    public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
        if (lock) {
            throw new OperationNotSupportedException("不支持的操作");
        }
        super.replaceAll(function);
    }

    public void setLock(boolean lock) {
        this.lock = lock;
    }
}
